package org.edu.modules.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.bcel.internal.generic.I2F;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.modules.oa.entity.DepartmentEntity;
import org.edu.modules.oa.entity.VeUserEntity;
import org.edu.modules.oa.entity.bo.BasicResponseBO;
import org.edu.modules.oa.entity.bo.DepartmentBo;
import org.edu.modules.oa.mapper.IVeBaseDepartmentMapper;
import org.edu.modules.oa.service.DepartmentService;
import org.edu.modules.oa.service.impl.DepartmentServiceImpl;
import org.springframework.cloud.stream.binder.rabbit.config.RabbitServiceAutoConfiguration;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 林彬辉
 * @version V1.0
 * @Package org.edu.modules.oa.controller
 * @date 2021/7/31 13:40
 */
@Api(tags="部门数据同步")
@RestController
@RequestMapping("oa/veXoaCommon")
@Slf4j
public class VeXoaCommonDepartmentController {
    @Resource
    private DepartmentService departmentService;
    //日志注解
    @AutoLog(value = "查看所有部门")
    @ApiOperation(value="查看所有部门", notes="查看所有部门")
    @PostMapping(value = "/synchronousDepartment")
    //获取所有部门
    public Result<?> synchronousDepartment(){
        BasicResponseBO<List<DepartmentBo>> listBasicResponseBO = departmentService.getDepartmentAll();
        List<DepartmentBo> list = listBasicResponseBO.getResult();
//        int isChildren = 0;
        String leader1 ="无上级主管领导";
        String leader2 ="无上级分管领导";
        boolean res = true;
        for (DepartmentBo departmentBo : list){
            res = boToEntity(departmentBo,leader1,leader2);
        }
        if (res){
            return Result.OK("部门同步成功");
        }else {
            return Result.error("部门同步失败");
        }


    }
    //递归同步部门数据
    public boolean boToEntity(DepartmentBo departmentBo,String leader1,String leader2){
        boolean res = true;

        DepartmentEntity departmentEntity = new DepartmentEntity();
        departmentEntity.setDeptId(departmentBo.getId());
        departmentEntity.setDeptParent(departmentBo.getPid());
        departmentEntity.setDeptName(departmentBo.getJgmc());
        departmentEntity.setDeptAbbrName(departmentBo.getJgjc());


        if (departmentBo.getJgdz()==null||"".equals(departmentBo.getJgdz())){
            departmentEntity.setDeptAddress("暂无地址信息");
        }else {
            departmentEntity.setDeptAddress(departmentBo.getJgdz());
        }

        departmentEntity.setTelNo(departmentBo.getTelephone());
        //未确定对应关系
//        if (departmentBo.getPid()==0){//是否全局部门
//            departmentEntity.setGDept(1L);
//
//        }else {
//            departmentEntity.setIsOrg("1");
//            departmentEntity.setClassifyOrg(1L);
//
//        }
        departmentEntity.setOrgAdmin("机构管理员");
        departmentEntity.setDeptFunc("部门职能");
        departmentEntity.setDeptCode(departmentBo.getJgh());
        departmentEntity.setDeptNo(departmentBo.getJgh());
        if ("".equals(departmentBo.getFgldName())||departmentBo.getFgldName() == null){
            departmentEntity.setManager("无部门主管");
        }else {
            departmentEntity.setManager(departmentBo.getFzrName());
        }

        if ("".equals(departmentBo.getFgldName())||departmentBo.getFgldName()==null){
            departmentEntity.setAssistantId("无部门助理");
        }else {
            departmentEntity.setAssistantId(departmentBo.getFgldName());
        }

        departmentEntity.setOrgAdmin("机构管理员");
        if ("".equals(leader1)||leader1==null){
            departmentEntity.setLeader1("无上级主管领导");
        }else {
            departmentEntity.setLeader1(leader1);
        }
        if ("".equals(leader2)||leader2==null){
            departmentEntity.setLeader2("无上级分管领导");
        }else {
            departmentEntity.setLeader2(leader2);
        }

//        departmentEntity.setWelinkDept(departmentBo.getId());


        QueryWrapper<DepartmentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("DEPT_ID",departmentBo.getId());
        DepartmentEntity d = departmentService.getOne(wrapper);
        if (d != null){

            res = departmentService.update(departmentEntity,wrapper);
        }else {
            res = departmentService.save(departmentEntity);

        }
        if (!res){
            return res;
        }

        if(departmentBo.getChildren().length>0){
            for (DepartmentBo bo : departmentBo.getChildren()){
//                departmentBo = bo;
                boToEntity(bo,departmentBo.getFzrName(),departmentBo.getFgldName());
            }

        }
        return res;
    }

}
